package com.example.multilevelapprovalback.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.metadata.CellExtra;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON;
import com.example.multilevelapprovalback.mapper.TeachProjMapper;
import com.example.multilevelapprovalback.pojo.TeachingProjectExcel;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import java.util.Map;
// TeachingProjectListener 不能被spring管理，要每次读取excel都要new，然后里面用到spring可以构造方法传进去
@Slf4j
public class TeachingProjectListener implements ReadListener<TeachingProjectExcel> {
    private Integer autoIncrement = 1;
    /**
     * 存储读取后的数据
     */
    private TeachProjMapper teachProjMapper;

    /**
     * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 100;
    /**
     * 缓存的数据
     */
    private List<TeachingProjectExcel> cacheTeachingProjectList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
    /**
     * 把spring管理的类传进来
     * @param teachProjMapper
     */
    public TeachingProjectListener(TeachProjMapper teachProjMapper) {
        log.info("我创建了");
        this.teachProjMapper = teachProjMapper;
    }
    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {
        ReadListener.super.onException(exception, context);
    }
    @Override
    public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) {
        ReadListener.super.invokeHead(headMap, context);
    }
    /**
     * 这个每一条数据解析都会来调用
     * @param teachingProjectExcel
     * @param analysisContext
     */
    @Override
    public void invoke(TeachingProjectExcel teachingProjectExcel, AnalysisContext analysisContext) {
        log.info("解析到一条数据：{}", JSON.toJSONString(teachingProjectExcel));
        String codeNumber = teachingProjectExcel.getJobNumber() + System.currentTimeMillis() +autoIncrement.toString();
        autoIncrement++;
        log.info("项目编码：{}",codeNumber);
        teachingProjectExcel.setCodeNumber(codeNumber);
        log.info("解析到一条数据：{}", JSON.toJSONString(teachingProjectExcel));
        cacheTeachingProjectList.add(teachingProjectExcel);
        // 达到BATCH_COUNT了，需要去存储一次数据库，防止数据几万条数据在内存，容易OOM
        if(cacheTeachingProjectList.size() >= BATCH_COUNT) {
            //存储数据库操作
            saveData();
            //存储完成清理 list
            cacheTeachingProjectList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }
    @Override
    public void extra(CellExtra extra, AnalysisContext context) {
        ReadListener.super.extra(extra, context);
    }
    /**
     * 所有数据解析完成了  都会来调用
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        // 这里也要保存数据，确保最后遗留的数据也存储到数据库
        saveData();
        log.info("所有数据解析完成！");
    }
    @Override
    public boolean hasNext(AnalysisContext context) {
        return ReadListener.super.hasNext(context);
    }
    /**
     * 加上存储数据库
     */
    public void saveData() {
        log.info("{}条数据，开始存储数据库！",cacheTeachingProjectList.size());
        teachProjMapper.batchInsert(cacheTeachingProjectList);
        log.info("存储数据库成功");
    }
}
